from time import time
import sys
import re
from collections import defaultdict

try:
  xmlfile = sys.argv[1]
  outfile = sys.argv[2]
  xmlsize = 2611567
except:
  print "%s <xmlfile> <outputfile>" % sys.argv[0]
  sys.exit(0)

def author_groups():
  author_re = re.compile(r'<author>(.*)</author>')
  author_group = []
  with open(xmlfile, 'r') as input:
    for line in input.xreadlines():
      m = author_re.match(line)
      if m:
        author = m.group(1)
        author_group.append(author)
      else:
        if author_group:
          yield author_group
          author_group = []

def author_pair_count():
  count = defaultdict(int)
  for (i, group) in enumerate(author_groups()):

    for a in group:
      for b in group:
        key = (a, b)
        count[key] += 1

    if i % 10000 == 0:
      print "%d: %.2f" % (i, float(i)/xmlsize)

  return count

if __name__ == '__main__':
  s = time()
  count = author_pair_count()
  print "Computed counts in %.2f minutes" % ((time()-s)/60.)
  print "Dumping out to a file."
  s = time()
  with open(outfile, 'w') as out:
    for ((a,b), c) in count.items():
      print >>out, "%s,%s,%d" % (a, b, c)
  print "Written to outfile in %.2f seconds" % ((time()-s))
